//
//  BookShelfViewController.m
//  FunRead
//
//  Created by  King on 15/5/22.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "BookShelfViewController.h"
#import "SANetWorkingTask.h"
#import "CollectClass.h"
#import "BookDetailController.h"
@interface BookShelfViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)UITableView *table;

// 书架的书来自于本地, 存放到数组中
@property(nonatomic, retain)NSMutableArray *tableArray;

          
@end

@implementation BookShelfViewController

- (void)dealloc
{
    [_tableArray release];
    [_table release];

    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ( self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.tableArray = [NSMutableArray array];
        self.arr = [NSMutableArray array];
        
        self.dic1 = [NSMutableDictionary dictionary];
        self.dic2 = [NSMutableDictionary dictionary];
        self.dic3 = [NSMutableDictionary dictionary];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    if (self.tableArray.count == 0) {
        self.tableArray = [NSMutableArray arrayWithArray:self.arr];
    }
    
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.view.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height );
    self.navigationItem.title = @"书架";

    [self createTable];
    
}
// 开始进入时显示
- (void)createArr{
   self.dic1 = [NSMutableDictionary dictionary];
   NSNumber *num1 = [NSNumber numberWithInt:134];
   [self.dic1 setObject:num1 forKey:@"bookid"];
  [self.dic1 setObject:@"凤惊天" forKey:@"bookName"];
  [self.dic1 setObject:@"http://images.xxsy.net/bimg/478653.jpg" forKey:@"imageUrl"];
   self.dic2 = [NSMutableDictionary dictionary];
   NSNumber *num2 = [NSNumber numberWithInt:1085];
    [self.dic2 setObject:num2 forKey:@"bookid"];
   [self.dic2 setObject:@"随波逐流之一代军师" forKey:@"bookName"];
   [self.dic2 setObject:@"http://image.cmfu.com/books/21839/21839.jpg" forKey:@"imageUrl"];

   self.dic3 = [NSMutableDictionary dictionary];
   NSNumber *num3 = [NSNumber numberWithInt:51430];
   [self.dic3 setObject:num3 forKey:@"bookid"];
   [self.dic3 setObject:@"仙源农场" forKey:@"bookName"];
   [self.dic3 setObject:@"http://image.cmfu.com/books/3352708/3352708.jpg" forKey:@"imageUrl"];

   self.arr = [NSMutableArray arrayWithObjects:self.dic1, self.dic2, self.dic3, nil];
   
}
#pragma mark-
#pragma mark- 请求数据
- (void)viewWillAppear:(BOOL)animated
{
    
    self.tableArray = [CollectClass readArray];
    if (self.tableArray.count == 0) {
        [self createArr];
        self.tableArray = [NSMutableArray arrayWithArray:self.arr];
        [CollectClass writeArray:self.dic1];
        [CollectClass writeArray:self.dic2];
        [CollectClass writeArray:self.dic3];
    }
    
}
#pragma mark -
#pragma mark - 创建table
- (void)createTable
{
    NSArray *array = [CollectClass readArray];
    self.tableArray = [NSMutableArray arrayWithArray:array];

    
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(5, 0, self.view.frame.size.width - 10, self.view.frame.size.height - 49 - 64) style:UITableViewStylePlain];
    [self.table setBackgroundColor:[UIColor whiteColor]];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    // 隐藏滑动条
    self.table.showsVerticalScrollIndicator = NO;
    // 开始设置headertable
    
    // 设置tableview的footer
    self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
  
    
    [self.table release];
}

#pragma mark - 点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 跳到每本书的详情, 传值bookid
    BookDetailController *detail = [[BookDetailController alloc]init];
    detail.str = [NSString stringWithFormat:@"%@", [[self.tableArray objectAtIndex:indexPath.row] objectForKey:@"bookid"]];
    
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}



#pragma mark - 删除时
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [CollectClass deleteArray:indexPath.row];
        self.tableArray = [CollectClass readArray];
//        [self.tableArray removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }

}


#pragma mark -
#pragma mark - required协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentity = @"cell";
    BookShelfCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (!cell) {
        cell = [[BookShelfCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentity];
    }
    cell.frame = CGRectMake(0, 0, self.view.frame.size.width, 80);
    // 点击不变色
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    // 在后面加大于号
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    
   
    [cell ViewWithData:[self.tableArray objectAtIndex:indexPath.row]];
    
    
    
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
#pragma mark - 返回分区数
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return 2;
}
#pragma mark - 返回标题数
#pragma mark - 标题







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
